using System;
using UnityEngine;
using UnityEngine.Video.UI;
using UnityEngine.UI;
using UnityEngine.Video;

public class UIVideoPlayerCtrl : MonoBehaviour
{
    private UIVideoPlayer uiVideoPlayer;

    private int index = 0;
    private string[] urls ={ "https://meta-oss.genimous.com/files/gaoxiaoshuiwu0920.mp4", "https://meta-oss.genimous.com/tax/gyxjzsqkczc.mp4", "https://meta-oss.genimous.com/files/202108nashuirenzhijia0920.mp4", "https://meta-oss.genimous.com/files/496851336105529.mp4"};
    void Start()
    {
        InitUIVideoPlayer(transform, ref uiVideoPlayer);
        
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            string url = urls[index];
            index++;
            index %= urls.Length;
            
            uiVideoPlayer.VideoPlayer.SetDirectAudioVolume(0,1);
            uiVideoPlayer.VideoPlayer.gameObject.SetActive(true);
            uiVideoPlayer.VideoPlayer.source = VideoSource.Url;
            uiVideoPlayer.VideoPlayer.url = url;
            uiVideoPlayer.VideoDisplay.texture = null;
            uiVideoPlayer.SetVisible(true);
            uiVideoPlayer.SetCloseButtonVisible(true);
            uiVideoPlayer.isShowUI = true;
        }
    }


    private void InitUIVideoPlayer(Transform go, ref UIVideoPlayer uiVideoPlayer)
    {
        uiVideoPlayer = go.Find("UIVideoPlayer").gameObject.GetOrAddCompument<UIVideoPlayer>();
        uiVideoPlayer.VideoPlayer = go.transform.Find("Video Player").GetComponent<VideoPlayer>();
        uiVideoPlayer.isShowUI = true;
        uiVideoPlayer._autoHide = false;

        Transform videoTrans = go.transform.Find("UIVideoPlayer");
        uiVideoPlayer._overlayManager = videoTrans.Find("Overlays").GetOrAddComponent<OverlayManager>();
        uiVideoPlayer._overlayManager._stalledImage = videoTrans.Find("Overlays/StalledRing").GetComponent<Image>();
        uiVideoPlayer._overlayManager._feedbackImage = videoTrans.Find("Overlays/Feedback/Foreground").GetComponent<Image>();
        uiVideoPlayer._overlayManager._feedbackCanvas = videoTrans.Find("Overlays/Feedback").GetComponent<CanvasGroup>();

        uiVideoPlayer._timelineTip = videoTrans.Find("Controls/Timline-Hovers").GetComponent<RectTransform>();

        uiVideoPlayer._canvasTransform = videoTrans.GetComponent<RectTransform>();
        uiVideoPlayer._sliderTime = videoTrans.Find("Controls/Timeline").GetComponent<Slider>();
        uiVideoPlayer._videoTouch = videoTrans.Find("Video/VideoDisplay").GetOrAddComponent<UnityEngine.EventSystems.EventTrigger>();
        uiVideoPlayer._controlsGroup = videoTrans.Find("Controls").GetComponent<CanvasGroup>();

        uiVideoPlayer._textTimeDuration = videoTrans.Find("Controls/BottomRow/TextTimeDuration").GetComponent<Text>();
        uiVideoPlayer._sliderVolume = videoTrans.Find("Controls/BottomRow/VolumeMask/SliderVolume").GetComponent<Slider>();
        uiVideoPlayer._buttonPlayPause = videoTrans.Find("Controls/BottomRow/ButtonPlayPause").GetComponent<Button>();
        uiVideoPlayer._buttonVolume = videoTrans.Find("Controls/BottomRow/ButtonVolume").GetComponent<Button>();
        uiVideoPlayer._buttonTimeBack = videoTrans.Find("Controls/BottomRow/ButtonNavBack").GetComponent<Button>();
        uiVideoPlayer._buttonTimeForward = videoTrans.Find("Controls/BottomRow/ButtonNavForward").GetComponent<Button>();

        uiVideoPlayer._buttonClose = videoTrans.Find("BtnClose").GetComponent<Button>();

        uiVideoPlayer._segmentsSeek = videoTrans.Find("Controls/Timeline/Fill Area/Fill-Seek").GetOrAddComponent<HorizontalSegmentsPrimitive>();
        uiVideoPlayer._segmentsSeek.color = new Color(81 / 255f, 81 / 255f, 81 / 255f, 255 / 255f);

        uiVideoPlayer._segmentsBuffered = videoTrans.Find("Controls/Timeline/Fill Area/Fill-Buffered").GetOrAddComponent<HorizontalSegmentsPrimitive>();
        uiVideoPlayer._segmentsBuffered.color = new Color(20 / 255f, 199 / 255f, 118 / 255f, 57 / 255f);

        uiVideoPlayer._segmentsProgress = videoTrans.Find("Controls/Timeline/Fill Area/Fill-Progress").GetOrAddComponent<HorizontalSegmentsPrimitive>();
        uiVideoPlayer._segmentsProgress.color = new Color(20 / 255f, 199 / 255f, 118 / 255f, 255 / 255f);
    }
}

public static class UnityEx
{
    public static T GetOrAddComponent<T>(this Transform uo) where T : Component
    {
        if (uo == null)
            return null;
        return uo.gameObject.GetOrAddCompument<T>();
    }

    public static T GetOrAddCompument<T>(this GameObject uo) where T : Component
    { 
        if (uo == null)
            return null;

        T t = uo.GetComponent<T>();
        if (t == null)
            t = uo.AddComponent<T>();

        return t;
    }
}